/*==================================================
Project:       Targeting Social Programs
Authors:       Diether W. Beuermann
               Bridget Hoffmann        
               Marco Stampini 
               David L. Vargas
               Diego Vera-Cossio
----------------------------------------------------
Creation Date:    15 Aug 2022 - 11:01:26
Modification Date:   
Do-file version:    01
References:          
Output:             
==================================================*/


 /*==================================================
               0: Program set up
 ==================================================*/
 *Written on STATA 17
 drop _all
 set varabbrev off	// no variable abbreviations allowed (personal preference)
 
 /*==================================================
               1: load and transformations
 ==================================================*/

 *---------- 1.1: Load data 
 use "${dir3r}/01_survey/replica/survey_targeting_r1_long.dta", clear

 ** safe guard to drop all existing variables that should be created by this do
 foreach v in _sample_srv l_pp_inc_srv ihs_pp_inc_srv cookpower_connected_srv prop_kids floor_finished_srv children_srv d_l_pp_inc_srv d_ihs_pp_inc_srv d_formal_work_srv d_informal_work_srv work_any_srv nat_shock job_loss_formal job_loss_informal d_reco_ilness_inc d_loss_illness_inc d_reco_cut_remittance_inc d_loss_cut_remittance_inc {
	cap drop `v'
 }
  
 *----------  1.2. Split sample

 /* Randomly split the relevant subsample for each country in half */
 sort id_vivienda_SISBEN
 set seed 339487731
 
 // An uniform have 50% probably of being > 0.5, let's use that for the split
 cap drop _random 
 gen  _random = runiform() if year == 2019
 bys id_hogar_SISBEN : egen _aux = max(_random)
 replace _random = _aux
 gen _sample_srv = 1
 replace _sample_srv = 2 if _random > 0.5
 lab var _sample_srv "Training or testing sample"
 lab define lsmaple 1 "Training" 2 "Testing", replace
 lab values _sample_srv lsmaple
 drop _random _aux
 
 *----------  1.3. Prices update 
 
 ** This is done in the main surevy clean up 

 *-----------1.4. var transformations

 //======= Income variables
gen ihs_pp_inc_srv = asinh(income_pp_nt) // Inverse hyperbolic sine of PP income net of transfers
gen l_pp_inc_srv = log(1000+income_pp_nt) // log of PP income net of transfers

 //======= Housing variables
 ** prepare housing variables
 egen cookpower_connected_srv =rowmax(housing_36a housing_36b)
 lab var cookpower_connected_srv "Cooking power gas or electric"
 
 egen floor_finished_srv = rowmax(housing_15a housing_15b) 
 lab var floor_finished_srv "Finished floors"
 
 //======= Housing variables
 cap rename housing_14a wall_finished_srv  
 cap rename housing_23a wc_flush_srv  	
 cap rename housing_26a running_water_srv  	
 cap rename housing_32a trash_service_srv  	
 cap rename housing_33	kitchen_srv  
 cap rename housing_16a electricity_srv  
 
 //======= Asset variables 
 cap rename assets_own_b own_washing_machine_srv
 cap rename assets_own_g own_tractor_srv
 cap rename assets_own_f own_moto_srv
 cap rename assets_own_h own_car_srv
 cap rename assets_own_c own_PC_srv
 cap rename assets_own_a own_fridge_srv

 //======= Labour 
 cap rename work_formal     formal_work_srv
 cap rename work_informal   informal_work_srv
 egen work_any_srv = rowmax(formal_work_srv informal_work_srv)
 lab var work_any_srv "Paid work"

 // demographics 
 cap rename hh_10c prop_kids_srv
 cap rename hh_14  age_feb20_srv
 cap rename hh_07 n_members_srv
 cap rename hh_16_elementary elementary_srv
 cap rename hh_16_high highschool_srv
 cap rename hh_16_tertiary tertiary_edu_srv

 /*----------------------------------------------------------------------
		-------------------- ADMIN DATA 	-----------------------
------------------------------------------------------------------------
 
 gen children_srv = hh_10b > 0 if !missing(hh_10b)
 gen prop_kids = n_kids_18 / n_members // missing in sisben 

 ----------------------------------------------------------------------
	-------------------- ADMIN DATA END 	-----------------------
------------------------------------------------------------------------*/

 //====== Other Shocks
 egen nat_shock = rowmax(nat_disaster fire)
 lab var nat_shock "Natural disaster or fire"

 *---------- 1.5. changes 
 tsset id_hogar_SISBEN (year) // declare time panel structure
 
 // Income 
 gen d_l_pp_inc_srv = D.l_pp_inc_srv
 gen d_ihs_pp_inc_srv = D.ihs_pp_inc_srv

 // losses 
 glo losses job_loss ilness death divorce bankrupt theft conflict cut_remittance fire nat_disaster nat_shock
 foreach v of global losses {	   
    cap drop d_`v'
    cap drop d_reco_`v'
    cap drop d_loss_`v'
    gen d_`v' = D.`v'
	loc vlab : var label `v'
	lab var d_`v' "Change: `vlab'"
    gen d_reco_`v' = d_`v' < 0 if !missing(d_`v')
    gen d_loss_`v' = d_`v' > 0 if !missing(d_`v')
    lab var d_reco_`v' "Recovery: `vlab'"
    lab var d_loss_`v' "Loss: `vlab'"
 }
 
 // work 
 foreach v in formal_work_srv informal_work_srv work_any_srv {	   
    cap drop d_`v'
    cap drop d_loss_`v'
    cap drop d_gain_`v'
    gen d_`v' = D.`v'
	loc vlab : var label `v'
	lab var d_`v' "Change: `vlab'"
    gen d_gain_`v' = d_`v' > 0 if !missing(d_`v')
    gen d_loss_`v' = d_`v' < 0 if !missing(d_`v')
    lab var d_gain_`v' "Recovery: `vlab'"
    lab var d_loss_`v' "Loss: `vlab'"
 }

 ** formal and informal job loss
 gen job_loss_formal = job_loss * l.formal_work_srv 
 gen job_loss_informal = job_loss * l.informal_work_srv

 *---------- 1.6. Intensity variable
  
 * join nat and fires 
 foreach t in a b c d e f g h i j k l z {
    cap drop nat_shock_`t'
    egen nat_shock_`t' = rowmax(fire_`t' nat_disaster_`t')
 }

 * calculate ratios
 foreach v in illness death bankrupt cut_remittance theft fire conflict nat_disaster nat_shock {
    cap drop `v'_incidence
    cap drop lag_`v'_incidence
    egen `v'_incidence = rowtotal(`v'_a	`v'_b	`v'_c	`v'_d	`v'_e	`v'_f	///
                                    `v'_g	`v'_h	`v'_i	`v'_j	`v'_k	`v'_l)
    replace `v'_incidence = . if missing(`v'_z) 
    replace `v'_incidence = `v'_incidence / 12

    gen lag_`v'_incidence = l.`v'_incidence
 }

 *---------- 1.7. Job inestability 

 ** Jobloss has no intensity, but let's create a better inestability measure
 cap drop job_volatitliy
 gen job_volatitliy = job_loss if inrange(job_loss_m,1,10) // leading months to job status question 
 replace job_volatitliy = 0 if job_loss_m == 11            // ignore if job change question month
 replace job_volatitliy = 1 if l.job_loss_m == 12 // if job loss takes place in december is a job loss for next term 
 replace job_volatitliy = 0 if missing(job_volatitliy) & !missing(job_loss)
 
 cap drop job_volat
 gen job_volat = job_loss if job_loss_m != 11 // simpler version 

 * new interactions 
 gen d_loss_illness_inc = illness_incidence * d_loss_ilness
 gen d_reco_ilness_inc = lag_illness_incidence * d_reco_ilness
 lab var d_loss_illness_inc	"Loss: Illness Incidence"
 lab var d_reco_ilness_inc	"Recovery: Illness Incidence"

 gen d_loss_cut_remittance_inc = cut_remittance_incidence * d_loss_cut_remittance
 gen d_reco_cut_remittance_inc = lag_cut_remittance_incidence * d_reco_cut_remittance
 lab var d_loss_cut_remittance_inc "Loss: Remittances Cut Incidence"
 lab var d_reco_cut_remittance_inc "Recovery: Remittances Cut Incidence"
 
 *---------- 1.8 Final save 
 compress 
 save "${dir3r}/01_survey/replica/survey_targeting_r1_long.dta", replace 
